package com.glsc.ngateway.platform.controller.glres;


import cn.hutool.core.collection.CollectionUtil;
import com.glsc.ngateway.common.api.platform.dto.user.PlatformUserDto;
import com.glsc.ngateway.common.api.platform.resp.PlatformResponse;
import com.glsc.ngateway.common.base.domain.mysql.gateway.platform.glres.GlscSystemAuth;
import com.glsc.ngateway.common.base.domain.mysql.gateway.platform.glres.GlscSystemPartner;
import com.glsc.ngateway.common.base.domain.mysql.gateway.platform.glres.GlscSystemResource;
import com.glsc.ngateway.common.base.domain.mysql.gateway.platform.it.ItDict;
import com.glsc.ngateway.common.base.domain.mysql.gateway.platform.it.ItDictData;
import com.glsc.ngateway.common.base.domain.mysql.gateway.system.SystemMain;
import com.glsc.ngateway.common.base.domain.mysql.gateway.system.SystemMember;
import com.glsc.ngateway.common.base.domain.mysql.opmanagemain.archive.CounterParty;
import com.glsc.ngateway.common.base.enums.SystemMemberTypeEnum;
import com.glsc.ngateway.platform.aspect.UserCache;
import com.glsc.ngateway.platform.dto.GlscSystemPartnerDto;
import com.glsc.ngateway.platform.dto.GlscSystemResourceDto;
import com.glsc.ngateway.platform.service.glres.GlscSystemAuthService;
import com.glsc.ngateway.platform.service.glres.GlscSystemPartnerService;
import com.glsc.ngateway.platform.service.glres.GlscSystemResService;
import com.glsc.ngateway.platform.service.it.DictionaryService;
import com.glsc.ngateway.platform.service.system.SystemMainService;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 资源管理-系统资源管理
 */
@RestController
@RequestMapping("/api/res/system")
@Slf4j
public class GlscSystemResourceController {

    @Resource
    private UserCache userCache;

    @Resource
    private GlscSystemResService glscSystemResService;

    @Resource
    private GlscSystemAuthService glscSystemAuthService;

    @Autowired
    private DictionaryService dictionaryService;

    @Resource
    private GlscSystemPartnerService glscSystemPartnerService;


    /**
     * 分页查找
     *
     * @param dto
     * @param request
     * @return
     */
    @PostMapping("/page")
    public PlatformResponse<Page<GlscSystemResource>> page(@RequestBody GlscSystemResourceDto dto, HttpServletRequest request) {

        Map<String, Object> param = new HashMap<>();
        if (StringUtils.isNotBlank(dto.getName())) {
            param.put("LIKE_name", dto.getName());
        }

        if (StringUtils.isNotBlank(dto.getSystemType())) {
            param.put("EQ_systemType", dto.getSystemType());
        }

        if (StringUtils.isNotBlank(dto.getSystemManageOaAccount())) {
            param.put("EQ_systemManageOaAccount", dto.getSystemManageOaAccount());
        }


        /*ItDictData dictData = dictionaryService.findByDictData("t_res_sys_all_auth_manage");
        List<ItDict> dictValues = dictData.getDictValues();
        Set<String> superOaAccounts = dictValues.stream().map(ItDict::getDictSubentry).collect(Collectors.toSet());
        PlatformUserDto user = userCache.get(request);
        if (!superOaAccounts.contains(user.getUsername())) {
            List<GlscSystemAuth> glscSystemAuths = glscSystemAuthService.listByUserId(user.getId());
            if (CollectionUtil.isNotEmpty(glscSystemAuths)) {
                Set<Integer> systemIdSet = glscSystemAuths.stream().map(GlscSystemAuth::getSystemId).collect(Collectors.toSet());
                param.put("IN_id", StringUtils.join(systemIdSet, ","));
            } else {
                return PlatformResponse.successData(Page.empty());
            }
        }*/

        Page<GlscSystemResource> page = glscSystemResService.page(dto.getPageNo(), dto.getPageSize(), param);

        if (Objects.nonNull(page) && CollectionUtil.isNotEmpty(page.getContent())) {

            for (GlscSystemResource sys : page.getContent()) {
                GlscSystemPartnerDto partnerDto = GlscSystemPartnerDto.builder().pageNo(1).pageSize(500).partnerType("out_system").sourceType("2").systemId(sys.getId()).build();
                Page<GlscSystemPartner> partnerPage = glscSystemPartnerService.page(partnerDto);
                if (Objects.nonNull(partnerPage) && CollectionUtil.isNotEmpty(partnerPage.getContent())) {
                    List<String> list = partnerPage.getContent().stream().map(GlscSystemPartner::getPartnerName).collect(Collectors.toList());
                    sys.setSystemNames(StringUtils.join(list, ","));
                }


            }
        }

        return PlatformResponse.successData(page);
    }

    @PostMapping("/listAllSimpleInfo")
    public PlatformResponse<List<GlscSystemResource>> listAllSimpleInfo(@ApiParam(required = false, value = "systemType") @RequestParam(required = false) String systemType) {
        return PlatformResponse.successData(glscSystemResService.listAllSimpleInfo(systemType));
    }


    /**
     * 根据id查找
     *
     * @param
     * @return
     */
    @GetMapping("/findById")
    public PlatformResponse<GlscSystemResource> findById(@ApiParam(required = false, value = "id") @RequestParam(required = true) Integer id, HttpServletRequest request) {
        PlatformUserDto user = userCache.get(request);
        GlscSystemResource systemRes = glscSystemResService.findById(id);
        systemRes.setOperateFlag(0);

        ItDictData dictData = dictionaryService.findByDictData("t_res_sys_all_auth_manage");
        List<ItDict> dictValues = dictData.getDictValues();
        Set<String> superOaAccounts = dictValues.stream().map(ItDict::getDictSubentry).collect(Collectors.toSet());
        if (superOaAccounts.contains(user.getUsername())) {
            systemRes.setOperateFlag(1);
        }
        if (user.getUsername().equals(systemRes.getSystemManageOaAccount())) {
            systemRes.setOperateFlag(1);
        }
        return PlatformResponse.successData(systemRes);
    }

    /**
     * 保存
     *
     * @param obj
     * @return
     */
    @PostMapping("/save")
    public PlatformResponse<?> save(@RequestBody GlscSystemResource obj, HttpServletRequest request) {
        PlatformUserDto user = userCache.get(request);
        glscSystemResService.save(obj, user);
        return PlatformResponse.success();
    }

    @PostMapping("/delete")
    public PlatformResponse<?> delete(@RequestBody GlscSystemResource obj) {
        glscSystemResService.delete(obj.getId());
        return PlatformResponse.success();
    }


}
